package com.xh.bussiness.tiku.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.jingyou.bean.request.JYPaperQueryRequest;
import com.xh.bussiness.jingyou.bean.request.JYPaperSaveRequest;
import com.xh.bussiness.jingyou.bean.request.JYSmartPaperSaveRequest;
import com.xh.bussiness.jingyou.service.IJYPaperService;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.bussiness.teachbook.util.PaperUtils;
import com.xh.bussiness.tiku.bean.dto.PaperDto;
import com.xh.bussiness.tiku.bean.request.*;
import com.xh.bussiness.tiku.model.Paper;
import com.xh.bussiness.tiku.service.IPaperService;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.util.FileUtils;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/3/14 15:18
 **/
@Api(tags = "题库-试卷接口")
@RestController
@RequestMapping("/tiku/paperDown")
public class PaperDownController {

    @Autowired
    private IPaperService paperService;
    @Autowired
    private IDictionaryService dictionaryService;

    private static final String URL_ROOT = "https://xinzhou.100bsf.cn/test";
    private static final String URL_LOCAL = "/Users/mac/Desktop/新舟试卷/";

    @GetMapping("/downDocPaper")
    @ExcludeLoginTokenIntercept
    @ApiOperation(value = "下载试卷", notes = "下载试卷")
    public void downDocPaper(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Dictionary> subjects = dictionaryService.queryByTypeCode("SUBJECT");
        Map<String, String> subjectNames = subjects.stream().collect(Collectors.toMap(Dictionary :: getCode, Dictionary :: getName));
        QueryWrapper<Paper> qw = new QueryWrapper<>();
        qw.eq("source_from", 3);
        qw.isNotNull("file_path");
        List<Paper> papers = paperService.queryByCondition(qw);
        for (Paper paper : papers) {
            String fileName = paper.getTitle() + "." + paper.getFilePath().split("\\.")[1];
            String fileUrl = URL_ROOT + paper.getFilePath();
            downloadByNIO(fileUrl, URL_LOCAL + subjectNames.get(paper.getSubjectId())+ "/", fileName);
            break;
        }
    }
    public static void downloadByNIO(String url, String saveDir, String fileName) {

        try (InputStream ins = new URL(url).openStream()) {
            Path target = Paths.get(saveDir, fileName);
            Files.createDirectories(target.getParent());
            Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
